﻿using Newtonsoft.Json;
using Station.Inspect.SuaModel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Vision.Common;

namespace Station.Inspect.UI
{
    
    public class NodeMark: CDrawRect
    {
        public int ID = 0;
        public string Name { get => $"Mark{ID}"; }
        public int Dx { get; set; } = 500;
        public int Dy { get; set; } = 500;
        public double Threshold { get; set; } = 0.05;
        public double AngleLimit { get; set; } = 5;

        [JsonIgnore]
        public ModelHNcc Model { get; set; } = new ModelHNcc();
        [JsonIgnore]
        public Bitmap SrcImage { get; set; } //识别前图像
        [JsonIgnore]
        public Bitmap DstImage { get; set; } //识别后图像
        private Bitmap image;
        [JsonIgnore]
        public Bitmap Image { get=> image; 
            set 
            {
                image = value;
                Model.Load(image);
            }
        }
        static protected Pen MarkBorderPen { get; set; } = new Pen(Color.Yellow, 2);//边框颜色
        static protected SolidBrush MarkBorderBrush { get; set; } = new SolidBrush(Color.FromArgb(80, 150, 150, 0));//边框颜色
        static protected SolidBrush MarkFillBrush { get; set; } = new SolidBrush(Color.FromArgb(80, 255, 255, 0));
        static protected Font font = new Font("微软雅黑", 25);
        public PointF BaseStartPoint { get; set; } = new PointF(0, 0);
        public float BaseAngle { get; set; } = 0;
        //复位至初始值
        public void Reset(bool bBase2Cur = true)
        {
            if (bBase2Cur)
            {
                StartPoint = BaseStartPoint;
                Angle = BaseAngle;
            }
            else
            {
                BaseStartPoint = StartPoint;
                BaseAngle = Angle;
            }
        }
        public override void Draw(Graphics g, float scale = 1)
        {
            if (m_visible == false)
                return;
            int startX = 0;
            int startY = 0;
            Rectangle rectangle = new Rectangle(startX, startY, (int)size.Width, (int)size.Height);
            if (scale > 1)
                scale = 1;
            Pen curPen = State == TrackerState.normal ? MarkBorderPen : BorderPenSelected;
            SolidBrush solidBrush = State == TrackerState.normal ? MarkFillBrush : FillBrushSelected;
            float crossWidth = Math.Min(size.Width / 2, 5 / scale);
            float crossHeight = Math.Min(size.Height / 2, 5 / scale);
            Matrix oldM = g.Transform;
            g.TranslateTransform(startPoint.X, startPoint.Y);
            if (Math.Abs(angle) > float.Epsilon)
                g.RotateTransform((float)angle);
            g.FillRectangle(MarkBorderBrush, - Dx, - Dy, Rect.Width + Dx * 2, Rect.Height + Dy * 2);
            g.FillRectangle(solidBrush, rectangle);
            
            g.DrawLine(curPen, size.Width / 2 - crossWidth, size.Height / 2, size.Width / 2 + crossWidth, size.Height / 2);
            g.DrawLine(curPen, size.Width / 2, size.Height / 2 - crossHeight, size.Width / 2, size.Height / 2 + crossHeight);
            g.DrawRectangle(curPen, rectangle);
            g.DrawString(Name, font,Brushes.Yellow, size.Width / 2 + 5, size.Height / 2 + 5);
            if (State == TrackerState.active)//选中
            {

                float cursize = 7 / scale;
                float dx = cursize / 2;
                float x1 = startX - dx;
                float x2 = startX + size.Width / 2 - dx;
                float x3 = startX + size.Width - dx;
                float y1 = startY - dx;
                float y2 = startY + size.Height / 2 - dx;
                float y3 = startY + size.Height - dx;

                g.FillRectangle(BorderBrush, x1, y1, cursize, cursize);   //左上
                g.FillRectangle(BorderBrush, x2, y1, cursize, cursize);  //上
                g.FillRectangle(BorderBrush, x3, y1, cursize, cursize);  //右上

                g.FillRectangle(BorderBrush, x1, y2, cursize, cursize);  //左中
                g.FillRectangle(BorderBrush, x3, y2, cursize, cursize);  //右中

                g.FillRectangle(BorderBrush, x1, y3, cursize, cursize);   //左下
                g.FillRectangle(BorderBrush, x2, y3, cursize, cursize);  //下
                g.FillRectangle(BorderBrush, x3, y3, cursize, cursize);  //右下
            }
            g.Transform = oldM;
        }
    }
}
